package com.haiqi.modules.map.service;

import com.haiqi.common.persistence.dao.WatersInfoMapper;
import com.haiqi.common.persistence.entity.WatersInfo;
import com.haiqi.common.utils.UUIDGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.apache.commons.lang.StringUtils;

import java.util.List;

/**
 * Created by HaiQi on 2017-2-20.
 */
@Service
public class WatersInfoService {
    @Autowired
    private WatersInfoMapper watersInfoMapper;

    /**
     * 保存水域信息
     * 判断水域信息对象的ID是否为空，如果为空，则新增一条水域信息，
     * 如果不为空，则根据该ID修改水域信息
     * @param watersInfo 水域信息对象
     * @return
     */
    public boolean save(WatersInfo watersInfo){
        boolean success=false;
        int i = 0;
        if (StringUtils.isNotEmpty(watersInfo.getId())){
            i = watersInfoMapper.update(watersInfo);
        }else{
            watersInfo.setId(UUIDGenerator.getUUID());
            i = watersInfoMapper.insert(watersInfo);
        }
        if (i>0){
            success = true;
        }

        return success;
    }

    /**
     * 更改水域的显示状态
     *
     * @param id 水域ID
     * @param reserve2 水域显示状态 1：显示；2：隐藏
     * @return
     */
    public boolean updateState(String reserve2,String id){
        int result=-1;
        result = watersInfoMapper.updateShowById(reserve2,id);
        if(result>0){
            return  true;
        }else{
            return false;
        }
    }
    /**
     * 删除一条水域信息
     * 根据传入的水域对象的ID删除一条水域信息
     * @param watersInfo 传入的水域信息对象
     * @return 如果删除成功，则返回true，否则返回false
     */
    public boolean delete(WatersInfo watersInfo){
        boolean success = false;
        int i = watersInfoMapper.delete(watersInfo.getId());
        if (i>0) {
            success=true;
        }
        return success;
    }

    /**
     * 删除一条水域信息
     * @param id 水域信息ID
     * @return
     */
    public boolean delete(String id){
        boolean success = false;
        int i = watersInfoMapper.delete(id);
        if (i>0) {
            success=true;
        }
        return success;
    }
    /**
     * 根据ID查找一条水域信息
     * @param id
     * @return
     */
    public WatersInfo getOneWatersInfo(String id){
        WatersInfo watersInfo = null;
        watersInfo = watersInfoMapper.selectById(id);
        return watersInfo;
    }

    /**
     * 根据水域类型获取所有的水域信息
     * @param type 水域类型
     * @return 返回所有的同一类型的水域信息
     */
    public List<WatersInfo> getAllByType(String type){
        List<WatersInfo> list = null;
        list = watersInfoMapper.selectByType(type);
        return list;
    }

    /**
     * 获取所有的水域信息
     * @return 返回水域信息列表
     */
    public List<WatersInfo> getAllWatersInfo(){
        List<WatersInfo> list = null;
        list = watersInfoMapper.selectAll();
        return list;
    }
}
